/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

/**
 * 拉黑/扣钱不发管理-拉黑快递员/驿站
 * */
import React, { useRef } from 'react';
import DragTable from '@/components/DragTable';
import ProTable from '@ant-design/pro-table';
import KbRefuseModal from '@/components/RefuseModal';
import { useSelector } from 'umi';
import type { ProColumns, ActionType } from '@ant-design/pro-table';
import type { ConnectState } from '@/models/connect';
import type { blcakAccount } from '@/pages/sms/blackDeductMoney/index.d';
import { platform } from '@/pages/sms/specialTmp/index.d';
import { getUsersList, setOptions, switchWhite } from '@/services/sms/blackDeductMoney';
import { ProFormText } from '@ant-design/pro-form';
import { message, Popconfirm, Space } from 'antd';
import styles from './table.less';

const Index: React.FC = () => {
  const { winSize } = useSelector((state: ConnectState) => state.global);
  const actionRef = useRef<ActionType>();
  const handleRequest = async (params: any) => {
    const { current: page, pageSize: limit, cm_phone, cm_id } = params;
    return getUsersList({ page, limit, cm_phone, cm_id }).then(({ code, data, msg }) => {
      if (code == 0) {
        return { data };
      } else {
        message.error(msg);
        return {};
      }
    });
  };
  const handleBlackClick = async (record: blcakAccount, url: string) => {
    const { kb_id, cm_id, phone: cm_phone } = record;
    const data: {
      kb_id: string;
      cm_id: string;
      cm_phone?: string;
    } = {
      kb_id,
      cm_id,
    };
    if (url !== '/Shield/adminUntied') {
      data.cm_phone = cm_phone;
    }
    const { code, msg } = await setOptions(url, data);
    if (code == 0) {
      actionRef.current?.reload();
      message.success('操作成功');
      return;
    }
    message.error(msg);
  };

  const handleSwitchWhite = (cm_id: string) => {
    switchWhite({ cm_id }).then((res) => {
      const { code, msg } = res;
      if (code == 0) {
        message.success('操作成功');
        actionRef.current?.reload();
      } else {
        message.error(msg);
      }
    });
  };
  const columns: ProColumns<blcakAccount>[] = [
    {
      title: '快递员/驿站账号',
      dataIndex: 'cm_phone',
      hideInTable: true,
      renderFormItem: () => {
        return <ProFormText placeholder='请输入手机号' />;
      },
    },
    {
      title: '平台标识',
      dataIndex: 'account_type',
      align: 'center',
      search: false,
      ellipsis: true,
      render: (_: any, record: blcakAccount) => {
        const txt = !record.account_type ? '-' : platform[record.account_type];
        return txt;
      },
    },
    {
      title: 'cm_id',
      align: 'center',
      dataIndex: 'cm_id',
      renderFormItem: () => {
        return <ProFormText placeholder='请输入cm_id' />;
      },
    },
    {
      title: '帐号',
      dataIndex: 'phone',
      align: 'center',
      search: false,
      ellipsis: true,
    },
    {
      title: '注册时间',
      dataIndex: 'create_time',
      align: 'center',
      search: false,
      ellipsis: true,
    },
    {
      title: '拉黑原因',
      dataIndex: 'black_note',
      align: 'center',
      search: false,
      ellipsis: true,
    },
    {
      title: '操作',
      dataIndex: 'options',
      align: 'center',
      search: false,
      ellipsis: true,
      width: winSize === 'xs' ? 80 : 230,
      fixed: 'right',
      render: (_: any, record: blcakAccount) => {
        const { is_deduct_not_send, kb_id, cm_id, phone: cm_phone, is_sms_white } = record;
        const blackUrl = '/Shield/removeSMSBlack';
        const deductUrl = record.is_deduct_not_send == 0 ? '' : '/Shield/adminUntied';
        return (
          <Space direction={winSize === 'xs' ? 'vertical' : 'horizontal'}>
            {record.is_black ? (
              <Popconfirm
                title={`你确定取消拉黑吗？`}
                onConfirm={handleBlackClick.bind(null, record, blackUrl)}
                key='approved'
              >
                <a>取消拉黑</a>
              </Popconfirm>
            ) : (
              <KbRefuseModal
                label='确定拉黑该快递员/驿站？'
                api='/Shield/addSMSBlack'
                name='note'
                tableRef={actionRef}
                formData={{
                  kb_id,
                  cm_id,
                  cm_phone,
                }}
              >
                <a>拉黑</a>
              </KbRefuseModal>
            )}
            {is_deduct_not_send ? (
              <Popconfirm
                title='你确定恢复下发吗？'
                onConfirm={handleBlackClick.bind(null, record, deductUrl)}
                key='reject'
              >
                <a>恢复下发</a>
              </Popconfirm>
            ) : (
              <KbRefuseModal
                label='确定扣钱不发？'
                api='/Shield/adminBlack'
                tableRef={actionRef}
                formData={{
                  kb_id,
                  cm_id,
                  cm_phone,
                }}
              >
                <a>扣钱不发</a>
              </KbRefuseModal>
            )}
            <Popconfirm
              title={is_sms_white == '0' ? '确定添加白名单' : '确定删除白名单'}
              onConfirm={handleSwitchWhite.bind(null, cm_id)}
            >
              <a>{is_sms_white == '0' ? '加入可信用户' : '取消可信用户'}</a>
            </Popconfirm>
          </Space>
        );
      },
    },
  ];
  return (
    <DragTable>
      <ProTable<blcakAccount>
        actionRef={actionRef}
        rowKey='id'
        request={handleRequest}
        manualRequest
        columns={columns}
        className={styles.deductMoney}
        pagination={{
          pageSize: 30,
          showQuickJumper: false,
          showSizeChanger: false,
        }}
        scroll={{ x: 640 }}
        options={false}
        search={{
          collapsed: false,
        }}
      />
    </DragTable>
  );
};

export default Index;
